//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.Research.DataLayer
{
    public class ProviderPkgInfo
    {
        public string ProviderName;
        public string ProviderKey;
        public ConnectionManagerDialog_Base Dialog;
        public ConnMgrDlg_Button Button;
        public List<IConnMgrInfoParams> Presets;
        private IConnMgrInfoParamsHelper helper;

        public ProviderPkgInfo(string name, string key, ConnectionManagerDialog_Base dialog,
            ConnMgrDlg_Button button, IConnMgrInfoParamsHelper helper)
        {
            this.ProviderName = name;
            this.ProviderKey = key;
            this.Dialog = dialog;
            this.Button = button;
            this.helper = helper;
            this.Presets = new List<IConnMgrInfoParams>();
        }

        public Connection GetConnectionByID(Guid id)
        {
            return this.Dialog.GetConnectionByID(id);
        }

        public IConnMgrInfoParams GetPresetByID(Guid id)
        {
            if (Presets == null || Presets.Count == 0)
                return null;

            foreach (IConnMgrInfoParams ds in Presets)
            {
                if (ds.ID.Equals(id))
                    return ds;
            }

            return null;
        }

        public override string ToString()
        {
            return ProviderName;
        }

        public void FromXml(datasource.Provider prov)
        {
            this.Presets.Clear();

            if (prov != null)
            {
                if (prov.DataSource != null)
                {
                    foreach (datasource.DataSource ds in prov.DataSource)
                    {
                        IConnMgrInfoParams preset = helper.FromXml(ds);
                        this.Presets.Add(preset);
                    }
                }
            }
        }

        public datasource.Provider ToXml()
        {
            if (Presets != null && Presets.Count > 0)
            {
                datasource.Provider provConfig = new datasource.Provider();
                provConfig.name = ProviderKey;

                List<datasource.DataSource> presetSources = new List<datasource.DataSource>();
                foreach (IConnMgrInfoParams preset in Presets)
                {
                    datasource.DataSource presetConfig = helper.ToXml(preset);
                    presetSources.Add(presetConfig);
                }
                provConfig.DataSource = presetSources.ToArray();

                return provConfig;
            }

            return null;
        }

        internal bool HandlesConnection(IConnMgrInfoParams conn)
        {
            if (helper == null)
                return false;

            return helper.HandlesConnection(conn);
        }

        internal Connection OpenConnection(IConnMgrInfoParams conn)
        {
            return helper.OpenConnection(conn);
        }

        internal bool TestConnection(IConnMgrInfoParams conn, out BackendStorageException error)
        {
            return helper.TestConnection(conn, out error);
        }
    }
}
